package org.itboys.mobile.service.mongo.web.meal;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mdl.entity.mongo.meal.MdlMealProduct;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/1
 * 描述：迈迪龙_套餐商品
 */
@Service
public class MdlMealProductService extends BaseMongoService<MdlMealProduct>{
    @Autowired
    private SundayProductMapper productMapper;
    @Resource(name = "mdlDs")
    private MongoDataSource mdlDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return mdlDataSource;
    }
    @Override
    protected Class<MdlMealProduct> getEntityClass() {
        return MdlMealProduct.class;
    }

    /**
     * 分页获取
     * @param request
     * @return
     */
    public PageResult<MdlMealProduct> selectMealProduct(HttpServletRequest request, Long mealId){
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("isDeleted", 0);
        param.put("mealId",mealId);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"");
        PageResult<MdlMealProduct> result = super.containpageQuery(request,containsparam,param);
        result.setData(packageDtoMuilt(result.getData()));
        return result;
    }
    /**
     * 新增或者编辑
     * @param mealProduct
     */
    public void saveMealProduct(MdlMealProduct mealProduct){
        if(mealProduct.getId()==0l){
            super.save(mealProduct);
        }else{
            super.updateExceptEmpty(mealProduct.getId(), mealProduct);
        }
    }
    /**
     * 查找单一
     * @param mealId
     * @return
     */
    public MdlMealProduct findOne(Long id,Long mealId){
        MdlMealProduct mealProduct=null;

        if(LongUtil.isNotZreo(id)){
            mealProduct=super.getById(id);
            mealProduct=packageDtoSingle(mealProduct,null);
        }else{
            mealProduct=new MdlMealProduct();
            mealProduct.setId(0);
            mealProduct.setMealId(mealId);
        }
        return mealProduct;
    }

    /**
     * 组装dto
     * @param mealProduct
     * @param product
     * @return
     */
    public MdlMealProduct packageDtoSingle(MdlMealProduct mealProduct,SundayProduct product){
        Map<String,Object> param = new HashMap<String,Object>();
        if(product==null){
            product = productMapper.find(mealProduct.getProductId());
        }
        mealProduct.setName(product.getName());
        mealProduct.setNumber(product.getNumber());
        mealProduct.setStandard(product.getStandard());
        mealProduct.setPrice(product.getPrice());
        return mealProduct;
    }

    /**
     * 组装多个
     * @param mealProducts
     * @return
     */
    public List<MdlMealProduct> packageDtoMuilt(List<MdlMealProduct> mealProducts) {
        List<MdlMealProduct> results = new ArrayList<MdlMealProduct>();
        List<Long> productIds = new ArrayList<Long>();
        for (MdlMealProduct mealProduct : mealProducts) {
            productIds.add(mealProduct.getProductId());
        }
        if (ListUtil.isNotNull(productIds)) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("ids", productIds);

            List<SundayProduct> products = new ArrayList<SundayProduct>();
            products = productMapper.select(param);
            for (MdlMealProduct mealProduct : mealProducts) {
                for (SundayProduct product : products) {
                    if (LongUtil.isNotZreo(mealProduct.getProductId()) &&
                            mealProduct.getProductId().longValue() == product.getId().longValue()) {

                        results.add(packageDtoSingle(mealProduct,product));
                        break;
                    }
                }
            }
        }
        return results;
    }
}
